import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class Jim():
    def __init__(self):
        self.chrome_options = Options()
        self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9221")
        self.browser = webdriver.Chrome(options=self.chrome_options,executable_path='C:\Program Files\Google\Chrome\Application\chromedriver.exe')#,executable_path='C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
        self.n = 1200
        self.m = 0
        self.shuru4=input('请输入需要获取的关键字,空格用+代替：')


    def shubiaogundong(self):
        self.browser.get('https://www.youtube.com/results?search_query={}'.format(self.shuru4).replace('&','%26'))
        time.sleep(3)
        while self.m < 20400:
            self.browser.execute_script(f"window.scrollBy(0,{self.n})")
            self.m += 10
            print(self.m, '*************************************', self.n)

            time.sleep(2)
            try:
                message=self.browser.find_element_by_xpath("//div[@id='contents']/ytd-message-renderer[@class='style-scope ytd-item-section-renderer']/yt-formatted-string[@id='message']").text
                print(message)
                if message=='No more results':
                    self.m=20500
            except Exception as g:
                print('you')


if __name__ == '__main__':
    run=Jim()
    run.shubiaogundong()